//
//  ZXXComposeToolBar.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/23.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit
//为了可读性 把button的tag值作为枚举值
enum ZXXComposeToolBarType: Int{
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Trend = 1003
    // 表情
    case Emoticon = 1004
    // +
    case Add = 1005
}

class ZXXComposeToolBar: UIView {
    
    /// 表情图片的按钮
    var emoticonButton: UIButton?
    
    var isEmoticon:Bool = false{
        didSet{
            if isEmoticon{
//                print("键盘图片")
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }else{
//                print("表情图片")
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
            
        }
    }

    
    /// 闭包 跳转到控制器
    var composeToolBarColsure: ((type: ZXXComposeToolBarType)->())?


    override init(frame: CGRect) {
        super.init(frame: frame)
//        设置UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //  MARK: -  按钮点击的方法
    @objc private func buttonClick(button: UIButton){
        composeToolBarColsure!(type:ZXXComposeToolBarType(rawValue: button.tag)!)
    }
    
//    //  MARK: -  设置UI
///   设置composeToolBar UI
    private func setupUI(){
        
//        添加控件
        // 添加控件
        let pictureButton = addChildButton("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        let addButton = addChildButton("compose_add_background", type: .Add)
        
        // 添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton!)
            make.leading.equalTo(mentionButton.snp_trailing)
        }
        
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
            make.leading.equalTo(trendButton.snp_trailing)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
        
    }
    //  MARK: -  懒加载控件
    ///添加按钮的方法
    private func addChildButton (imageName:String,type:ZXXComposeToolBarType) -> UIButton {
        let button = UIButton()
        button.tag = type.rawValue
//        添加点击事件
        button.addTarget(self, action:#selector(ZXXComposeToolBar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
//        给button 设置图片
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
//        设置Button的北京图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
//        添加到父控件上
        addSubview(button)
        return button
        
    }

}
